package com.financing.platform.web.api;

import java.util.Collections;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.platform.common.Pager;
import com.financing.platform.common.ResultCode;
import com.financing.platform.common.ResultMap;
import com.financing.platform.dto.FpShopSubjectUserDTO;
import com.financing.platform.service.IFpShopSubjectUserService;

@Controller("fpShopSubjectUserController")
@RequestMapping("/api/fpShopSubjectUser")
public class FpShopSubjectUserController extends BaseAction  { 

	private Logger logger = Logger.getLogger(getClass());
	
	@Autowired
	private IFpShopSubjectUserService fpShopSubjectUserService;
	
	@RequestMapping("/listFpShopSubjectUser.shtml")
	@ResponseBody
	public ResultMap list(HttpServletRequest request, FpShopSubjectUserDTO fpShopSubjectUser,Pager pager){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("query fpShopSubjectUser list begin .......");
			
			fpShopSubjectUser.setBegin(pager.getStartIndex());
			fpShopSubjectUser.setPageSize(pager.getPageSize());
			
			int count = fpShopSubjectUserService.queryListCount(fpShopSubjectUser);
			
			pager.setTotalCount(count);
			
			List<FpShopSubjectUserDTO> fpShopSubjectUserList = null;
			
			if(count > 0){
				fpShopSubjectUserList = fpShopSubjectUserService.queryList(fpShopSubjectUser);
			}else{
				fpShopSubjectUserList = Collections.emptyList();
			}
			result.setAttribute("pager", pager);
			result.setAttribute("list", fpShopSubjectUserList);
			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("query fpShopSubjectUser list end .......");
		}catch(Exception e){
			log.error("query fpShopSubjectUser error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	
	@RequestMapping("/addFpShopSubjectUser")
	@ResponseBody
	public ResultMap add(HttpServletRequest request, FpShopSubjectUserDTO fpShopSubjectUser){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("add fpShopSubjectUser  begin .......");
			
//			DTOdateSet.setCreateInfo(fpShopSubjectUser);
//			DTOdateSet.setUpdateInfo(fpShopSubjectUser);
			
			int id = fpShopSubjectUserService.insert(fpShopSubjectUser);
			result.setAttribute("id", id);
			
			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("add fpShopSubjectUser list end .......");
		}catch(Exception e){
			log.error("add fpShopSubjectUser error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	
	
	@RequestMapping("/detailFpShopSubjectUser")
	@ResponseBody
	public ResultMap detail(HttpServletRequest request, FpShopSubjectUserDTO fpShopSubjectUser){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("query detail fpShopSubjectUser  begin .......");
			
			Integer id = fpShopSubjectUser.getShopSubjectUserId();
			
			if(id==null){
				result.setStatusCode(ResultCode.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			FpShopSubjectUserDTO dto = fpShopSubjectUserService.loadById(id);
			
			result.setAttribute("dto",dto);
			
			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("query detail fpShopSubjectUser  end .......");
		}catch(Exception e){
			log.error("query detail fpShopSubjectUser error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	
	@RequestMapping("/deleteFpShopSubjectUser")
	@ResponseBody
	public ResultMap delete(HttpServletRequest request, FpShopSubjectUserDTO fpShopSubjectUser){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("delete fpShopSubjectUser  begin .......");
			
			Integer id = fpShopSubjectUser.getShopSubjectUserId();
			
			if(id==null){
				result.setStatusCode(ResultCode.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			fpShopSubjectUserService.delete(id);
			
			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("delete fpShopSubjectUser  end .......");
		}catch(Exception e){
			log.error("delete fpShopSubjectUser error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	
	@RequestMapping("/updateFpShopSubjectUser")
	@ResponseBody
	public ResultMap update(HttpServletRequest request, FpShopSubjectUserDTO fpShopSubjectUser){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("update fpShopSubjectUser  begin .......");
			
			Integer id = fpShopSubjectUser.getShopSubjectUserId();
			
			if(id==null){
				result.setStatusCode(ResultCode.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
//			DTOdateSet.setUpdateInfo(fpShopSubjectUser);
			
			fpShopSubjectUserService.update(fpShopSubjectUser);
			
			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("update fpShopSubjectUser list end .......");
		}catch(Exception e){
			log.error("update fpShopSubjectUser error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	
	@RequestMapping("/updateForceFpShopSubjectUser")
	@ResponseBody
	public ResultMap updateForce(HttpServletRequest request, FpShopSubjectUserDTO fpShopSubjectUser){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("updateForce fpShopSubjectUser  begin .......");
			
			Integer id = fpShopSubjectUser.getShopSubjectUserId();
			
			if(id==null){
				result.setStatusCode(ResultCode.STATUS_CODE_PARAMETERS_LOSE);
				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
//			DTOdateSet.setUpdateInfo(fpShopSubjectUser);
			
			fpShopSubjectUserService.updateForce(fpShopSubjectUser);
			
			
			result.setStatusCode(ResultCode.RESULT_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("updateForce fpShopSubjectUser list end .......");
		}catch(Exception e){
			log.error("updateForce fpShopSubjectUser error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultCode.RESULT_CODE_SYSTEM_ERROR);
		}
		
		return result;
	}
	
}
